package westh.ilib.struts.action.myTagCloud;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;

import westh.config.Config;
import westh.ilib.bean.tagBean.TagSys;
import westh.ilib.service.tagClouds.TagCloudsService;
import westh.log.YyError;


/**
 * @author ChaiLinglu
 * 
 * @ModifyDate 2010-09-14
 * @modifier ChaiLinglu
 * @content refine
 * 
 */
public class GetMyTagCloudsAction extends Action {
	private final static Log log = LogFactory.getLog(GetMyTagCloudsAction.class);
	private PrintWriter out = null;
	//get clouds Ip address
	String localhostUrl = Config.get("localhostUrl");
	String remoteUrl = Config.get("remoteUrl");
	
	public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {

		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		DynaActionForm f = (DynaActionForm) form;
		//get data from page
		String dateStart = "";
		String dateEnd = "";
		int tagNum = 0 ;
		String userId=request.getRemoteUser();
		userId="tagCloudUser";///////////////////////////////////////////////////tempTest
		String module="myTagCloud";
		
		//create input json for CDO
		JSONObject resultObject = new JSONObject();
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("userId", userId);
		jsonObj.put("module", module);
		jsonObj.put("area", "");
		resultObject.put("methodName", "getTagSetting");
		resultObject.put("jsonStr", jsonObj);
		String inputStr = resultObject.toString();
		
		// call webService  --  CDO
		String outString = "";
		try {
			MuleClient clientD = new MuleClient();
			UMOMessage umo = clientD.send("axis:http://"+localhostUrl+":4381/LIBJson/iDlibJson?method=rulesControl",inputStr, null);
			outString = (String) umo.getPayload();
			log.debug("getTagSetting:Output:CDO:"+outString);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		String retCode=""; //return code
		String retMsg="";//return message
		String returnStr="";//

		JSONObject jsonOut= JSONObject.fromObject(outString);
		retCode = jsonOut.getString("retCode");
		if (retCode.equals("0")) {
			String result = jsonOut.getString("result");
			JSONArray resultArray = JSONArray.fromObject(result);
			
			TagSys tagsys = new TagSys();
			List list = new ArrayList();
			if(resultArray.size()!=0){//user set value
				for(int i=0;i<resultArray.size();i++){
					tagsys = new TagSys();
					JSONObject jsonOut2 = JSONObject.fromObject(resultArray.get(i));
					dateStart = jsonOut2.getString("queryDateStart");
					dateEnd = jsonOut2.getString("queryDateEnd");
					tagNum = jsonOut2.getInt("tagDispNum") ;
				}
			}else{//default value
				dateStart = TagCloudsService.getThreeMonthsBefore();
				dateEnd = TagCloudsService.getCurrentDate();
				tagNum = 8;
			}
		}else{//default value
			dateStart = TagCloudsService.getThreeMonthsBefore();
			dateEnd = TagCloudsService.getCurrentDate();
			tagNum = 8;
//			
			retMsg = jsonOut.getString("retMsg");
			request.setAttribute("info", retMsg);
			JSONObject json = new JSONObject();
			json.put("info",retMsg);
			out.println(json);
			return null;
//			return mapping.findForward("fail");
		}
		
		//create input json for tagsysCloud
		JSONObject tagInput=new JSONObject();
		tagInput.put("queryDateStart", dateStart);
		tagInput.put("queryDateEnd", dateEnd);
		tagInput.put("tagDispNum", tagNum);
		tagInput.put("userId", userId);
		
		// call webService  --  tagsysCloud
		String outTagString = "";
		try {
			MuleClient clientD = new MuleClient();
			log.info("tagInput="+tagInput.toString());
			UMOMessage umo = clientD.send("axis:http://"+remoteUrl+":4481/DataCenter/TagDataCenter?method=getMyTagCloud",tagInput.toString(), null);
			outTagString = (String) umo.getPayload();
			log.debug("getMyTagCloud:Output:tagsysCloud:"+outTagString);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		List list = new ArrayList();

		JSONObject jsonTagOut= JSONObject.fromObject(outTagString);
		String retTagCode = jsonTagOut.getString("retCode");
		if (retTagCode.equals("5500")) {
			String result = jsonTagOut.getString("result");
			JSONArray resultArry = JSONArray.fromObject(result);
			
//test 测试数据			
			TagSys t1 = new TagSys();
			TagSys t2 = new TagSys();
			t1.setTagName("hello");
			t1.setTagTimes("8");
			t2.setTagName("world");
			t2.setTagTimes("4");
			resultArry.add(t1);
			resultArry.add(t2);
//test			
			
			TagSys tagsys = new TagSys();
			for(int i=0;i<resultArry.size();i++){
				tagsys = new TagSys();
				JSONObject jsonOut2 = JSONObject.fromObject(resultArry.get(i));
				String tagName = jsonOut2.getString("tagName");
				String tagTimes = jsonOut2.getString("tagTimes");
				tagsys.setTagName(tagName);
				tagsys.setTagTimes(tagTimes);
				list.add(tagsys);
			}
		}else{
			YyError.show(retTagCode);
			retMsg = jsonOut.getString("retMsg");
			request.setAttribute("info", retMsg);
			JSONObject json = new JSONObject();
			json.put("info", retMsg);
			out.println(json);
			return null;
//			return mapping.findForward("fail");
			
		}
		
		request.setAttribute("list", list);
		JSONObject json = new JSONObject();
		json.put("list", list);
		out.println(json);
		return null;
//		return mapping.findForward("succ");
	}
}
